# 自动取款机模拟
# 第一周作业3)
# 班级：Python五期
# 学员：李子坚

# 定义一个用于存放账户信息的列表变量
account_list = [
                {'account_ID':'zhangsan','password':'111111','balance':1000.0},
                {'account_ID':'lisi','password':'222222','balance':2000.0},
                {'account_ID':'wangwu','password':'111111','balance':800.0}]

# 定义一个自动取款机登陆函数
def login(account_ID, password):
    '''
    自动取款机的登陆函数
    '''
    for account in account_list:
        if account['account_ID'] == account_ID and account['password'] == password:
            # 账号和密码匹配
            print("="*19, "登陆成功！", "="*19)
            return account

    # 没有匹配的账号和密码
    print("="*16, "账号或密码错误！", "="*16)
    return False

# 定义一个输入取款金额的函数
def input_cash_withdrawal():
    '''
    输入取款金额的函数
    '''
    while True:

        # 输出取款功能的初始界面
        print("")
        print("="*22, "取款", "="*22)
        print("{0:<1} {1:<15} {2:<15} {3:<15}".format(" ", "1.￥100元", "2.￥200元", "3.￥500元"))
        print("{0:<1} {1:<15} {2:<13} {3:<15}".format(" ", "4.￥1000元", "5.其他金额","6.返回"))
        print("="*50)
        
        key = input("请选择对应的菜单：")
        # 根据键盘值，判断并执行对应的操作
        if key == "1":              #1.￥100元
            return 100.0
        elif key == "2":            #2.￥200元
            return 200.0
        elif key == "3":            #3.￥500元
            return 500.0
        elif key == "4":            #4.￥1000元
            return 1000.0
        elif key == "5":            #5.其他金额
            try:
                # 获取输入的取款金额
                cash_withdrawal = float(input("请输入取款金额（元）：￥"))
            except ValueError:
                # 输入非法数值
                print("=============== 取款金额有误！ ===============")
                continue
            
            if cash_withdrawal > 3000.0:
                # 取款金额超过单次取款限额
                print("=========== 取款金额超过单次取款限额！ ===========")
                continue                

            elif cash_withdrawal > 0.0:
                return cash_withdrawal

            else:
                # 取款金额 <= 0.0
                print("=============== 取款金额有误！ ===============")
                continue                
        elif key == "6":            #6.返回
            # 取款金额为0，相当于没取款返回
            return 0.0
        else:
            print("=============== 无效的键盘输入！ =================")
            


# 自动取款机登录，循环3次
for i in range(0, 3):
    print("")
    print("="*19, "自动取款机", "="*19)
    print("="*16, "请输入账号和密码", "="*16)
    user_account_ID = input("账号：")
    user_password = input("密码：")

    # 调用自动取款机的登陆函数
    user_account = login(user_account_ID, user_password)
    # 账号和密码匹配，跳出循环
    if user_account:
        login_success = True
        break
else:
    print("======== 连续3次登陆失败，自动退出系统！ =========")
    login_success = False

# 自动取款机功能选择
while login_success:

    # 输出初始界面
    print("")
    print("="*19, "自动取款机", "="*19)
    print("{0:<1} {1:<15} {2:<15}".format(" ", "1.查询余额", "2.取款"))
    print("{0:<1} {1:<17} {2:<15}".format(" ", "3.存款", "4.退出系统"))
    print("="*50)
    
    key = input("请选择对应的菜单：")
    # 根据键盘值，判断并执行对应的操作
    if key == "1":
        #1.查询余额
        print("您的账户余额为：￥{:.2f}元".format(user_account['balance']))
        input("按任意键继续：")

    elif key == "2":
        #2.取款
        user_cash_withdrawal = input_cash_withdrawal()
        if user_cash_withdrawal > 0.0:
            if user_cash_withdrawal <= user_account['balance']:
                user_account['balance'] -= user_cash_withdrawal
                print("取款￥{:.2f}元成功！您的账户余额为：￥{:.2f}元".format(user_cash_withdrawal, user_account['balance']))
            else:
                print("========== 取款失败，您的账户余额不足！ ==========")
            input("按任意键继续：")
        
    elif key == "3":
        #3.存款
        try:
            # 获取输入的存款金额
            user_amount_deposited = float(input("请输入存款金额（元）：￥"))
        except ValueError:
            # 输入非法数值
            print("================= 存款金额有误！ =================")
            input("按任意键继续：")
            continue
            
        if user_amount_deposited > 0.0:
            user_account['balance'] += user_amount_deposited
            print("存款￥{:.2f}元成功！您的账户余额为：￥{:.2f}元".format(user_amount_deposited, user_account['balance']))
        else:
            print("================= 存款金额有误！ =================")
            
        input("按任意键继续：")

    elif key == "4":
        #4.退出系统
        print("="*22,"再见","="*22)
        break

    else:
        print("================= 无效的键盘输入！ ===================")
